<template>
	<view>
		<navbar :isback="true" title="提现"></navbar>
		<view class="top">
			<view class="u-flex u-flex-y-center u-flex-between" style="margin-bottom: 40rpx;">
				<view class="top__title">
					提现金额
				</view>
				<view class="top__btn" @click="value=m">
					全部提现
				</view>
			</view>
			<view class="u-flex u-flex-items-end">
				<view class="top__unit">
					￥
				</view>
				<input type="number" name="" id="" class="top__inp" v-model="value">
			</view>
		</view>
		<u-gap bgColor="#FAF9F9" height="19rpx"></u-gap>
		<view class="con">
			<view class="con__title">
				提现方式
			</view>
			<view class="pay">
				<view class="pay__item">
					<view class="u-flex u-flex-y-center">
						<u-icon name="/static/my/vx.png" size="83rpx"></u-icon>
						<view class="pay__item__name">
							微信
						</view>
					</view>
					<view class="pay__item__checkbox">
						<!-- <u-icon name="/static/my/wx.png" size="35rpx"></u-icon> -->
						<u-icon name="/static/my/yxw.png" size="35rpx"></u-icon>
					</view>
				</view>
			</view>
			<view class="con__title" style="margin: 20rpx 0;">
				收款码
			</view>
			<u-upload :fileList="fileList1" @afterRead="afterRead" @delete="deletePic" name="1" multiple
				:maxCount="1"></u-upload>
		</view>
		<view class="footer">
			<u-button text="确认提现" :loading="loading" @click="submit" shape='circle'
				color="linear-gradient(91deg, #6E77FA, #5E77FD)" size="large"
				:customStyle="{height:'114rpx'}"></u-button>
		</view>
	</view>
</template>

<script>
	import {
		BASE_URL
	} from '@/api/config.js'
	import user from '@/api/_user.js'
	export default {
		data() {
			return {
				value: 0,
				m: 0,
				fileList1: [],
				loading: false
			};
		},
		onLoad(o) {
			this.m = o.money
		},
		methods: {
			submit() {
				console.log(this.fileList1)
				if (!this.value) {
					uni.$u.toast('请输入提现金额')
					return
				}
				if (this.fileList1.length!=1) {
					uni.$u.toast('请上传收款码')
					return
				}
				
				this.loading = true
				user.submitWithdraw({
					price: this.value,
					image:this.fileList1[0].url
				}).then(res => {
					if (res.data.code == 1) {
						uni.$u.toast('提现成功')
						setTimeout(() => {
							this.loading = false
							uni.navigateBack()
						}, 1000)
					} else {
						this.loading = false
					}
				})
			},
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: JSON.parse(result).data.url
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url:BASE_URL+'/api/Common/upload', // 仅为示例，非真实的接口地址
						filePath: url,
						name: 'file',
						header: {
							"token": uni.getStorageSync('xxcstoken')
						},
						success: (res) => {
							setTimeout(() => {
								resolve(res.data)
							}, 1000)
						}
					});
				})
			},
		}
	}
</script>

<style lang="scss">
	.top {
		height: 240rpx;
		display: flex;
		flex-direction: column;
		justify-content: center;
		padding: 0 30rpx;

		&__title {
			font-family: AlibabaPuHuiTi_2_55_Regular;
			font-weight: 400;
			font-size: 30rpx;
			color: #333333;
			line-height: 30rpx;
		}

		&__btn {
			font-family: PingFang SC;
			font-weight: 500;
			font-size: 24rpx;
			color: #010101;
			line-height: 24rpx;
		}

		&__unit {
			font-family: PingFang SC;
			font-weight: 500;
			font-size: 40rpx;
			color: #2C2E2C;
			line-height: 50rpx;
		}

		&__inp {
			font-family: AlibabaPuHuiTi_2_65_Medium;
			font-weight: 500;
			font-size: 64rpx;
			color: #333333;
			margin-left: 10rpx;
			height: 70rpx;
		}
	}

	.con {
		padding: 40rpx 30rpx;

		&__title {
			font-family: Adobe Heiti Std;
			font-weight: normal;
			font-size: 32rpx;
			color: #333333;
			line-height: 32rpx;
		}

		.pay {
			&__item {
				margin-top: 36rpx;
				display: flex;
				align-items: center;
				justify-content: space-between;

				&__name {
					font-family: AlibabaPuHuiTi_2_45_Light;
					font-weight: 400;
					font-size: 32rpx;
					color: #333333;
					margin-left: 22rpx;
				}
			}
		}
	}

	.footer {
		position: fixed;
		padding: 0 50rpx 127rpx;
		bottom: 0;
		left: 0;
		width: 650rpx;
	}
</style>